/**
 * @author 黄波
 * 创建日期：2011Oct 8, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.util.ArrayList;
import java.util.List;

import com.platform.bean.PageBean;
import com.platform.os.bo.AccessBo;
import com.platform.os.util.Util;
import com.platform.pojo.ResInfo;
import com.platform.pojo.ResType;
import com.platform.pojo.UserInfo;
import com.platform.res.bean.ResInfoBean;
import com.platform.res.bo.ResBo;
import com.platform.user.bo.UserBo;

/**
 * 根据类别，加载资源列表
 */
public class LoadResListAction extends NJCCSuport {

	private String type;	//资源类别ID
	private List<ResInfoBean> reslist = new ArrayList<ResInfoBean>();	//根据类别查询出的资源信息列表
	private PageBean pageBean;	//分页实体
	private ResType typeItem;	//資源列表頁所顯示的類別

	private List<ResType> alltypes = new ArrayList<ResType>();	//所有资源类型列表

	private List<ResType> types = new ArrayList<ResType>();	//一级资源类型列表
	private List<ResType> typelist1 = new ArrayList<ResType>();	//资源子类型列表1
	private List<ResType> typelist2 = new ArrayList<ResType>();	//资源子类型列表2
	private List<ResType> typelist3 = new ArrayList<ResType>();	//资源子类型列表3
	private List<ResType> typelist4 = new ArrayList<ResType>();	//资源子类型列表4
	private List<ResType> typelist5 = new ArrayList<ResType>();	//资源子类型列表5

	private List<ResInfoBean> favRes = new ArrayList<ResInfoBean>();	//下载排行资源信息

	@Override
	public String execute() throws Exception {
		if(type == null || type.equals("")) {
			return ERROR;
		}

		ResBo resBo = ResBo.getInstance();
		UserBo userBo = UserBo.getInstance();
		//获得一级资源类型列表
		alltypes = resBo.getTypes();
		types = resBo.getTypes("0");

		//查询二级资源类型列表
		if(types != null) {
			if(types.size() > 0 && types.get(0) != null) {
				typelist1 = resBo.getTypes(types.get(0).getId().toString());
			}
			if(types.size() > 1 && types.get(1) != null) {
				typelist2 = resBo.getTypes(types.get(1).getId().toString());
			}
			if(types.size() > 2 && types.get(2) != null) {
				typelist3 = resBo.getTypes(types.get(2).getId().toString());
			}
			if(types.size() > 3 && types.get(3) != null) {
				typelist4 = resBo.getTypes(types.get(3).getId().toString());
			}
			if(types.size() > 4 && types.get(4) != null) {
				typelist5 = resBo.getTypes(types.get(4).getId().toString());
			}
		}

		//查询下载排行资源列表
		List<ResInfo> faResInfoList = (List<ResInfo>) resBo.getFaResInfo()[1];
		for(ResInfo item : faResInfoList) {
			ResInfoBean bean = new ResInfoBean();
			bean.setInfo(item);
			if(alltypes != null) {
				ResType tmp = getTypeName(item.getTypeId(), alltypes);
				if(tmp != null) {
					bean.setTypeName(tmp.getName());
				} else {
					bean.setTypeName("课程资源");
				}
			}
			UserInfo user = userBo.getUser(item.getUploadUserId().toString());
			if(user != null) {
				bean.setUserName(user.getRealName());
			} else {
				bean.setUserName(null);
			}
//			bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
			bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));
			favRes.add(bean);
		}
		
		//根据类别ID，查询该类别下的所有资源
		Object[] objs = resBo.getInfosByType(type, pageBean);
		pageBean = (PageBean) objs[0];
		List<ResInfo> infos = (List)objs[1];
		for(ResInfo item : infos) {
			ResInfoBean bean = new ResInfoBean();
			bean.setInfo(item);
			if(alltypes != null) {
				bean.setTypeName(getTypeName(item.getTypeId(), alltypes).getName());
			}
			UserInfo user = userBo.getUser(item.getUploadUserId().toString());
			if(user != null) {
				bean.setUserName(user.getRealName());
			} else {
				bean.setUserName(null);
			}
//			bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
			bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));
			reslist.add(bean);
		}
		typeItem = getTypeName(Integer.parseInt(type), alltypes);
		return SUCCESS;
	}

	/**
	 * @param typeId
	 * @return
	 */
	private ResType getTypeName(Integer typeId, List<ResType> types) {
		for(ResType item : types) {
			if(item.getId() == typeId) {
				return item;
			}
		}
		return null;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<ResInfoBean> getReslist() {
		return reslist;
	}

	public void setReslist(List<ResInfoBean> reslist) {
		this.reslist = reslist;
	}

	public List<ResType> getAlltypes() {
		return alltypes;
	}

	public void setAlltypes(List<ResType> alltypes) {
		this.alltypes = alltypes;
	}

	public List<ResType> getTypes() {
		return types;
	}

	public void setTypes(List<ResType> types) {
		this.types = types;
	}

	public List<ResType> getTypelist1() {
		return typelist1;
	}

	public void setTypelist1(List<ResType> typelist1) {
		this.typelist1 = typelist1;
	}

	public List<ResType> getTypelist2() {
		return typelist2;
	}

	public void setTypelist2(List<ResType> typelist2) {
		this.typelist2 = typelist2;
	}

	public List<ResType> getTypelist3() {
		return typelist3;
	}

	public void setTypelist3(List<ResType> typelist3) {
		this.typelist3 = typelist3;
	}

	public List<ResType> getTypelist4() {
		return typelist4;
	}

	public void setTypelist4(List<ResType> typelist4) {
		this.typelist4 = typelist4;
	}

	public List<ResType> getTypelist5() {
		return typelist5;
	}

	public void setTypelist5(List<ResType> typelist5) {
		this.typelist5 = typelist5;
	}

	public List<ResInfoBean> getFavRes() {
		return favRes;
	}

	public void setFavRes(List<ResInfoBean> favRes) {
		this.favRes = favRes;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public ResType getTypeItem() {
		return typeItem;
	}

	public void setTypeItem(ResType typeItem) {
		this.typeItem = typeItem;
	}
	
}
